<?php

namespace App\Http\Controllers\Admin;

use App\Exports\FangOwnersExport;
use App\Models\FangOwners;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class FangOwnersController extends BaseController
{
    /**
     * Display a listing of the resource.
     * 查询所有的房东信息
     * @return \Illuminate\Http\Response
     * 
     * 
     */
    public function index()
    {
        $data = FangOwners::paginate($this->pagesize);
        return view('admin.fangowners.index',compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     * 添加房东页面展示
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.fangowners.create');
    }

    /**
     * Store a newly created resource in storage.
     * 
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //先进行验证
        $this->validate($request,[
            'name' => 'required',
            'phone' => 'required',
            'card' => 'required'
        ]);
        //验证通过后，将数据传递过来
        $post = $request->except(['_token','file']);
        //这里还需要对传递的图片的路径进行处理
        $post['pic'] = trim($request->post('pic'),'#');
        FangOwners::create($post);
        return redirect(route('admin.fangowners.create'))->with('success', '添加成功');
    }


    //

    //上传房东身份证
    public function upfile(Request $request)
    {
        //上传文章封面
        $pic = config('up.pic');
        //如果确实文件进行文件上传
        if ($request->hasFile('file')) {
            //将文件进行上传处理
            $ret = $request->file('file')->store('', 'fangowners');
            //获取文件上传的路径
            $pic = '/uploads/fangowners/' . $ret;
        }
        //如果文件上传成功，将路径进行返回
        return ['status' => 0, 'url' => $pic];
    }


    //删除照片信息
    public function delpic(Request $request){
        //传递过来的地址
        $pic_url = $request->get('file');
        //得到真实的地址
        $file_path = public_path().$pic_url;
        unlink($file_path);
        return ['status' => 0,'msg' => '删除成功'];
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\FangOwners  $fangOwners
     * @return \Illuminate\Http\Response
     * 这里有个坑  show  最后面的参数是   fangowner   所以参数中也必须是fangowner
     */
    public function show(FangOwners $fangowner)
    {
        $pic_str = $fangowner->pic;
        $pic_list = explode('#',$pic_str);
        if(count($pic_list) == 0){
            echo '暂无照片数据';
            die;
        }
        array_map(function($item){
            echo "<ul style='list-style-type:none;width:500px;'>
                <li style='float:left;margin-left:30px;'>
                    <img src='{$item}' style='width:100px;height:80px;border:1px solid #ccc;' />
                </li>
            </ul>";
        },$pic_list);
    }

    //导出excel
    public function exports(){
        return Excel::download(new FangOwnersExport(), 'fangdong.xls');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\FangOwners  $fangOwners
     * @return \Illuminate\Http\Response
     */
    public function edit(FangOwners $fangOwners)
    {
        
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\FangOwners  $fangOwners
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, FangOwners $fangOwners)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\FangOwners  $fangOwners
     * @return \Illuminate\Http\Response
     */
    public function destroy(FangOwners $fangOwners)
    {
        //
    }
}
